using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YayiMgr.Ability.Docking.Commom;
using YayiMgr.Ability.Docking.UserApp;
using YayiMgr.Ability.Docking.UserApp.Dto;
using YayiMgr.Domain.SystemBase;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.UserApp
{
    public class UserServicer: AbilityServicer, IUserServicer
    {
        public IRepository<User> UserRepo { get; set; }
        public async Task<ResponseDto<LoginSuccessDto>> CheckLogin(string account, string password)
        {
            ResponseDto<LoginSuccessDto> responseDto = null;
            User user = await User.CheckLogin(account, password, UserRepo);
            if (user != null)
            {
                LoginSuccessDto dto = ModelMapper.Map<User, LoginSuccessDto>(user);
                dto.Token = Guid.NewGuid().ToString();
                responseDto = ResponseDto<LoginSuccessDto>.CreateSuccess(dto, "登录成功");
            }
            else 
            {
                responseDto = ResponseDto<LoginSuccessDto>.CreateFail("用户名或密码错误");
            }
            return responseDto;
        }

        public async Task<ResponseDto<UserRegisterDto>> Register(UserRegisterCreateDto createDto)
        {
            ResponseDto<UserRegisterDto> responseDto = null;
            User user = await UserRepo.GetAsync(u=>u.Account== createDto.Account);
            if(user!=null)
            {
                responseDto = ResponseDto<UserRegisterDto>.CreateFail($"手机号：{createDto.Account} 已经在本系统注册过，请登录。");
            }
            else
            {
                user = ModelMapper.Map<UserRegisterCreateDto,User>(createDto);
                user = await UserRepo.InsertAsync(user);
                UserRegisterDto Data = ModelMapper.Map<User,UserRegisterDto>(user);
                responseDto = ResponseDto<UserRegisterDto>.CreateSuccess(Data, "登录成功");
            }
            return responseDto;
        }



        
        public async Task<QueryListDto<UserDto>> GetUsers(UserQueryDto dto)
        {
            QueryListDto<UserDto> listDto = null;
            IQueryable<User> users = UserRepo.AsQueryable();
            if(dto.CompanyId!=null)
                users = users.Where(u=>u.CompanyId==dto.CompanyId);
            if(dto.Account!="")
                users = users.Where(u=>u.Account==dto.Account);
            if(dto.UserName!="")
                users = users.Where(u=>u.UserName.Contains(dto.UserName));
            if(dto.RoleID!=null)
                users = users.Where(u=>u.RoleID==dto.RoleID);

            List<User> users1 = await users.OrderBy(p=>p.Account).Skip((dto.PageIndex-1)*dto.PageSize).Take(dto.PageSize).ToListAsync();
            List<UserDto> Data =  ModelMapper.Map<List<User>,List<UserDto>>(users1);

            int total = await users.CountAsync();
            listDto = QueryListDto<UserDto>.Create(Data, total, dto.PageIndex, dto.PageSize);
            return listDto;
        }
        public async Task<ResponseDto<UserDto>> GetUserById(Guid id)
        {
            User user = await UserRepo.GetAsync(id);
            if(user==null)
                return ResponseDto<UserDto>.CreateFail("用户不存在");
            var dtos = ModelMapper.Map<User,UserDto>(user);
            return ResponseDto<UserDto>.CreateSuccess(dtos);
        }
        public async Task<ResponseDto<UserDto>> AddUser(UserCreateDto input)
        {
            var item = ModelMapper.Map<UserCreateDto, User>(input);
            User User = await UserRepo.InsertAsync(item);
            var dtos = ModelMapper.Map<User,UserDto>(User);
            return ResponseDto<UserDto>.CreateSuccess(dtos);
        }
        public async Task<ResponseDto<UserDto>> UpdateUser(Guid id, UserUpdateDto input)
        {
            User User = await UserRepo.GetAsync(id);
            if(User==null)
                return ResponseDto<UserDto>.CreateFail("用户不存在");
            User = ModelMapper.Map<UserUpdateDto, User>(input);
            User = await UserRepo.UpdateAsync(User);
            var dtos = ModelMapper.Map<User,UserDto>(User);
            return ResponseDto<UserDto>.CreateSuccess(dtos);
        }
        public async Task DeleteUser(Guid id)
        {
            await UserRepo.DeleteAsync(id);
        }

    }
}